package com.myblog.controller;

import com.myblog.pojo.Wechat;
import com.myblog.service.Wechatservice;
import com.myblog.utils.JsonData;
import jdk.nashorn.internal.ir.RuntimeNode;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.Date;

@Controller
@RequestMapping("/wechat")
public class Wechatcontroller {
    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private Wechatservice wechatservice;

    @RequestMapping("/attestation")
    public Object attestation(){
       return "attestation" ;
    }

    @GetMapping("/authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl){
        String url="http://codelong.natapp1.cc/wechat/userinfo";
        String redirectURL=wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_USER_INFO, URLEncoder.encode(returnUrl));
        System.out.println(redirectURL);
        return "redirect:"+redirectURL;
    }

    @GetMapping("/userinfo")
    public String userinfo(HttpServletRequest request, @RequestParam("code") String code, @RequestParam("state") String returnUrl) throws Exception{
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken=new WxMpOAuth2AccessToken();
        try{
            wxMpOAuth2AccessToken=wxMpService.oauth2getAccessToken(code);
        }catch (WxErrorException e){
            throw new Exception(e.getError().getErrorMsg());
        }
        System.out.println(wxMpOAuth2AccessToken);
        String openid=wxMpOAuth2AccessToken.getOpenId();
        WxMpUser wxMpUser=wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken,null);
        System.out.println(wxMpUser);
        Wechat wechat=new Wechat();
        wechat.setOpenId(wxMpUser.getOpenId());
        wechat.setWxname(wxMpUser.getNickname());
        wechat.setSex(wxMpUser.getSex());
        wechat.setCity(wxMpUser.getCity());
        wechat.setProvince(wxMpUser.getProvince());
        wechat.setImageurl(URLEncoder.encode(wxMpUser.getHeadImgUrl()));
        wechat.setTime(new Timestamp(new Date().getTime()));
        wechat.setAccount(1);
        int count=wechatservice.findcount(wxMpUser.getOpenId());
        System.out.println("count:"+count);
        if(count==0){
            wechatservice.insertwechat(wechat);
        }
        else{
            int account=wechatservice.selectaccount(wechat.getOpenId());
            wechat.setAccount(account+1);
            wechatservice.update(wechat);
        }
        HttpSession session=request.getSession();
        session.setAttribute("wechat",wechat);
        return "redirect:"+returnUrl+"?openid="+openid;
    }

    @GetMapping("/invalidate")
    @ResponseBody
    public Object invalidate(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.invalidate();
        System.out.println("-----invalidate------");
        return JsonData.buildSuccess();
    }
}
